<template>
	<view >
		<MyInput 
			label="昵称" 
			v-model="nickName" 
			:placeholder="userInfo.nick_name_by_dormitory"/>
	</view>
</template>

<script setup>
	import { ref , watch , defineEmits} from "vue"
	
	import MyInput from '@/components/MyInput/MyInput.vue'
	
	import {DormitoryModel} from '@/service/model/dormitory.js'
	
	const props=defineProps({
		clickSubmitNickNameFlag:{
			type:Number
		},
		userInfo:{
			type:Object
		},
		dormitoryAllId:{
			type:Object
		}
	})
	
	const dormitoryAllId=uni.getStorageSync('homeData')
	
	const emits=defineEmits(['setNickNameOk'])
	
	watch(()=>props.clickSubmitNickNameFlag,()=>{
		submitNickName()
	})
	
	const nickName = ref('')
	
	const dormitoryModel = new DormitoryModel()
	
	//提交修改名字
	const submitNickName=async()=>{
		
		if(!nickName.value.trim()){
			uni.$emit('tips','请输入有效的昵称!','warn')
			return
		}
		
		const {data,success} = await dormitoryModel.patchSetNickNameByDormitory({
			'nick_name_by_dormitory':nickName.value
		},dormitoryAllId.relation_id,dormitoryAllId.dormitory_id)
		
		if(success){
			nickName.value=''
			uni.$emit('tips',data,'success')
			emits('setNickNameOk')
		}
	}
</script>

<style lang="less">
	
</style>